﻿using System;

namespace PropertyExpression.ControllableQuery.Test
{
    public static class Paging2
    {
        public static QBuilder Rows(Action<QBuilder> mainQuery, Action<QBuilder> orderBy,
            Param<int> start, Param<int> finish)
        {
            return new QBuilder(@"
SELECT  *
FROM    (SELECT *,
                ROW_NUMBER() OVER (ORDER BY ", orderBy, @") AS RowNumber
         FROM   (", mainQuery, @"
                ) _
        ) _
WHERE   RowNumber BETWEEN ", start, @" AND ", finish, @"
");
        }

        public static QBuilder Count(Action<QBuilder> mainQuery)
        {
            return new QBuilder(@"
SELECT  COUNT(*) AS Value
FROM    (", mainQuery, @"
        ) _");
        }

        public static Tuple<QBuilder, QBuilder> All2(Action<QBuilder> mainQuery, Action<QBuilder> orderBy,
            Param<int> start, Param<int> finish)
        {
            return Tuple.Create(Rows(mainQuery, orderBy, start, finish), Count(mainQuery));
        }

        public static QBuilder All(Action<QBuilder> mainQuery, Action<QBuilder> orderBy,
            Param<int> start, Param<int> finish)
        {
            return new QBuilder(@"
SELECT  *
FROM    (SELECT *,
                ROW_NUMBER() OVER (ORDER BY ", orderBy, @") AS RowNumber
         FROM   (", mainQuery, @"
                ) _
        ) _
WHERE   RowNumber BETWEEN ", start, @" AND ", finish, @"

SELECT  COUNT(*) AS Value
FROM    (", mainQuery, @"
        ) _");
        }
    }

    public class Paging<TRows, TCount>
    {
        private readonly TRows rows;
        private readonly TCount count;

        public Paging(TRows rows, TCount count)
        {
            this.rows = rows;
            this.count = count;
        }

        public TRows Rows
        {
            get { return rows; }
        }

        public TCount Count
        {
            get { return count; }
        }
    }
}